<template>
	<satri-page>
		<view class="page">
			<view class="form">
				<satri-form label-position="top">
					<satri-form-item label="昵称">
						<satri-input
							v-model="form.nickName"
							type="nickname"
							:border="false"
							:height="80"
							:border-radius="12"
							placeholder="请填写昵称"
						/>
					</satri-form-item>
				</satri-form>
			</view>

			<satri-footer>
				<satri-button custom type="primary" :loading="loading" @tap="save"> 保存 </satri-button>
			</satri-footer>
		</view>
	</satri-page>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { useSatri, useStore } from "/@/satri";
import { useUi } from "/$/satri-ui";
import { onReady } from "@dcloudio/uni-app";

const { router } = useSatri();
const { user } = useStore();
const ui = useUi();

const loading = ref(false);

const form = reactive({
	nickName: "",
});

async function save() {
	loading.value = true;

	await user.update(form).catch((err) => {
		ui.showToast(err.message);
	});

	loading.value = false;

	ui.showTips("用户信息保存成功", () => {
		router.back();
	});
}

onReady(() => {
	form.nickName = user.info?.nickName || "";
});
</script>

<style lang="scss" scoped>
.page {
	.form {
		padding: 20rpx 24rpx;
	}
}
</style>
